package com.vinfai.demo.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vinfai.common.dao.IEntityDAO;
import com.vinfai.common.exception.DataAccessException;
import com.vinfai.common.service.impl.BaseService;
import com.vinfai.demo.dao.IUserDAO;
import com.vinfai.demo.service.IUserService;
import com.vinfai.hibernate4.entity.User;

@Service("UserService")
@Transactional
public class UserService extends BaseService<User, Long> implements IUserService{
	
	private IUserDAO userDAO;
	
	@Autowired
	@Qualifier("UserDAO")
	@Override
	public void setEntityDAO(IEntityDAO<User, Long> entityDAO) {
		this.entityDAO = entityDAO;
		this.userDAO = (IUserDAO)entityDAO;
	}

	@Override
	public String validateLogin(String name, String password) {
		String sql = "SELECT T.NAME,T.PASSWORD from T_USER T WHERE T.NAME=?";
		List<Object[]> list = userDAO.findNativeQuery(sql, 0, -1, new Object[]{name});
//		Object[] objs = userDAO.findUniqueByNativeSQL(sql, new Object[]{name});
		if(list!=null&&list.size()>0){
			Object[] objs = list.get(0);
			if(objs!=null){
				//解密
				String pwd = "123";
				if(!pwd.equals(password)){ 
					return "密码不正确";
				}
			}
		}else{
			return "用户不存在";
		}
		return "TRUE";
	}
	
	public String insert(User user){
		userDAO.save(user);
		User user2 = new User();
		user2.setName("user2");
		//user2.getEmail().toCharArray();
		userDAO.save(user2);
		return null;
	}
	
}
